package com.lyn.solitude_plugin_host;

import com.alibaba.fastjson.JSON;
import com.lyn.solitude_aidl_host.PluginHostAidlInterface;
import com.lyn.solitude_plugin_api.entity.Plugin;
import java.util.List;
import java.util.Objects;

/**
 * 插件宿主
 * @author longyn 2024/5/10
 * @version 1.0.0
 */
public class PluginHost {

    private static PluginHostAidlInterface pluginHostAidlInterface;

    public static void setPluginHostAidlInterface(PluginHostAidlInterface pluginHostAidlInterface) {
        PluginHost.pluginHostAidlInterface = pluginHostAidlInterface;
    }

    public static List<Plugin.Simple> loadAll(){
        try {
            Objects.requireNonNull(pluginHostAidlInterface,"宿主服务未初始化");
            String load_all_json_str = pluginHostAidlInterface.loadAll();
            return JSON.parseArray(load_all_json_str, Plugin.Simple.class);
        }catch (Throwable ignored){}
        return null;
    }

    public static int remove(String id){
        try {
            return pluginHostAidlInterface.remove(id);
        }catch (Throwable ignored){}
        return -1;
    }

    public static int open(String id){
        try {
            return pluginHostAidlInterface.open(id);
        }catch (Throwable ignored){}
        return -1;
    }

    public static int jump(String id, String className) {
        try {
            return pluginHostAidlInterface.jump(id, className);
        }catch (Throwable ignored){}
        return -1;
    }
}
